var express = require('express');
var router = express.Router();
// const connection = require('../../db/db');
const connection = require('../db/db.js');

//  全局查询数据
var funSelect = function(luyou,selectName){
  router.get(luyou,function(req,res,next){
    let sql = selectName
    connection.query(sql,function(error,result,fields){
      if(error){
        res.json({//xhr.status:获取当前服务器的响应状态  200=>成功
        status:400,message:"请求失败"})
      }else{
        res.json({
          //xhr.status:获取当前服务器的响应状态  200=>成功
          status:200,
          message:result
        })
      }
    })
  })
  } 
  // 全局查询data数据
funSelect("/api/findData","SELECT data.*,`sort`.sortName,`contentsort`.ctName FROM sort inner join  data on sort.sort_id = data.Sort_id inner join contentsort ON data.contentSort_id = contentsort.consort_id  ORDER BY data_time DESC")
// 全局查询知识标签数据
funSelect("/api/getknowTags","SELECT * FROM `sort`")
// 全局查询内容标签数据
funSelect("/api/getcontentTags","SELECT * FROM `contentsort`");
// 按时间降序查询data数据
// SELECT * FROM `data` ORDER BY `knowledgeSort_id` ASC
funSelect("/api/getDataDesc","SELECT * FROM `data` ORDER BY `data_time` DESC");
// 全局查询emoji
funSelect("/api/emoji","SELECT * FROM `emoji`");

// 全局查找userType为1 的信息：getBloggerMes
funSelect("/api/getBloggerMes","SELECT * FROM `users` where userType = 1");
// 全局查询友链：selLinks
funSelect("/api/selLinks","SELECT * FROM `links`");
// 全局查询用户：getAllUser
funSelect("/api/getAllUser","SELECT * FROM `users`");
// 全局查询用户：selSchool
funSelect("/api/selSchool","SELECT * FROM `school`");
// 全局用户一级评论点赞情况：selReviewPraise
// funSelect("/api/selReviewPraise","SELECT * FROM `messageboard` inner join users on messageboard.from_uid = users.user_id where praiseType = 1");
// 全局用户点赞博文情况：selArticlePraise
// funSelect("/api/selArticlePraise","SELECT * FROM `data` where dataPraise = '1'");
// 全局用户点赞博文情况：selTCollect
// funSelect("/api/selTCollect","SELECT * FROM `data` where dataCollect = '1'");
// 全局查询评论：getCommentarys
funSelect("/api/getCommentarys","SELECT * FROM `messageboard");
// 全局更改留言表信息状态为已读：updateAllCheck
funSelect("/api/updateAllCheck","UPDATE `messageboard` SET `mesIsCheck` = '1'");
// 输入一个变量的操作
var funSelZD = function(luyou,selectName){
  router.post(luyou,function(req,res,next){
    let key = req.body.variate;
    // console.log("---------ii");
    // console.log(key);
    let sql = selectName;
    connection.query(sql,key,function(error,result,fields){
      if(error){
        res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
        status:400,message:"请求失败"})
      }else{
        res.json({
          //xhr.status:获取当前服务器的响应状态  200=>成功
          status:200,
          message:result
        })
      }
    })
  })
  }
// 条件查询data中指定的知识标签数据-这是通过label点击标签获取的id
funSelZD("/api/getktDetails","SELECT * FROM `data` where Sort_id = ?");
funSelZD("/api/delPraiseData","DELETE FROM `datapraise` WHERE `dataPraiseId` = ?");
funSelZD("/api/delPraiseMes","DELETE FROM `praiseme` WHERE `praiseMeId` = ?");
funSelZD("/api/delCollectData","DELETE FROM `datacollect` WHERE `dataCollectId` = ?");
// 条件查询data中指定的内容标签数据
funSelZD("/api/getctDetails","SELECT * FROM `data` where contentSort_id = ?");
// 条件查询data详情
// funSelZD("/api/getDataDetail","SELECT * FROM `data` where data_id = ?");
// getstName-这是通过点击首页的sort标签获取的stName
// select * from 表1 inner join 表2 on 表1.公共字段=表2.公共字段
//select data.*,sort.sortName from data inner join sort on data.Sort_id = sort.id where sortName = "数据库技术与应用"
funSelZD("/api/getstName","select data.*,sort.sortName from data inner join sort on data.Sort_id = sort.sort_id where sortName = ?");
// getctName-这里是通过点击首页的contentSort标签获取的ctName
//select data.*, `contentsort`.ctName from data inner join contentsort on data.contentSort_id = contentsort.id where ctName = "野生技术协会"
funSelZD("/api/getctName","select data.*, `contentsort`.ctName from data inner join contentsort on data.contentSort_id = contentsort.consort_id where ctName = ?");
// 条件查询user详情(注册账户时，先查询该账户是否已经被注册):selectUser
funSelZD("/api/selectUser","SELECT `user_id`, `account`, `password`,`userType`,`nickname`,avatar FROM `users` where account = ?");
// 登录账号：loginUser
funSelZD("/api/selPersonal","SELECT * FROM `users` where user_id = ?");
// selSecret 
funSelZD("/api/selSecret","SELECT * FROM `secret` where user_id = ?");
// updateMesCheck 
funSelZD("/api/updateMesCheck","UPDATE `messageboard` SET `mesIsCheck` = '1' WHERE `mes_id` = ?");
// delUser  delete from stu where sid = '2';级联删除
funSelZD("/api/delUser","DELETE FROM `users` where user_id = ?");
// selPraise
// funSelZD("/api/selPraise","select * from `praise` where uid =  ?");
// selCollectL
funSelZD("/api/selCollectL","select * from `collect` where uid =  ?");
// getReNickName
funSelZD("/api/getReNickName","select users.nickname from `users` inner join `replymessage` on replymessage.from_userId =  users.user_id where reId =  ?");
// selChild
funSelZD("/api/selChild","SELECT * FROM `menu_children` where menu_id =  ?");
// getReplyUser
funSelZD("/api/getReplyUser","SELECT users.nickname FROM `messageboard` inner join users on users.user_id = messageboard.from_uid where messageboard.mes_id = ?");
// selPraisemeMes
funSelZD("/api/selPraisemeMes","SELECT * FROM messageboard  inner join `praiseme` on messageboard.mes_id = praiseme.Mmes_id inner join users on users.user_id = praiseme.praise_userId WHERE messageboard.from_uid  = ? ORDER BY praiseme.praiseme_time DESC");
// delPraisemeMes
funSelZD("/api/delPraisemeMes","DELETE FROM `praiseme` WHERE `praise_userId` = ?");
// selpraisemecount
funSelZD("/api/selpraisemecount","SELECT * FROM `praisemecount` WHERE `pmc_user_id` = ?");
// selPraiseDetails
funSelZD("/api/selPraiseDetails","select * from messageboard inner join  praiseme  on praiseme.Mmes_id = messageboard.mes_id inner join users on praiseme.praise_userId = users.user_id where messageboard.from_uid = ?");
// selNewPraiseMes
funSelZD("/api/selNewPraiseMes","SELECT * FROM messageboard  inner join `praiseme` on messageboard.mes_id = praiseme.Mmes_id inner join users on users.user_id = praiseme.praise_userId WHERE praiseme.`isCheck` = '0' AND  messageboard.from_uid  = ?");
// selNewReplyMes
funSelZD("/api/selNewReplyMes","select * from replymessage inner join messageboard on messageboard.mes_id = replymessage.to_uids INNER join users on users.user_id = messageboard.from_uid WHERE replymessage.replyIsCheck = '0' and messageboard.from_uid = ?");
// selLoginOut
funSelZD("/api/selLoginOut","SELECT * FROM `loginout` WHERE `login_user_id` = ?");
// updatePraiseMeIsCheck
funSelZD("/api/updatePraiseMeIsCheck","UPDATE praiseme set `isCheck` = '1' WHERE `praiseMeId` = ?");
// updateReplyMeIsCheck
funSelZD("/api/updateReplyMeIsCheck","UPDATE `replymessage` SET `replyIsCheck`= '1' WHERE `reId` = ?");
// selectReplymessage
funSelZD("/api/selectReplymessage","select * from `users` inner join `replymessage` on replymessage.from_userId =  users.user_id inner join messageboard on messageboard.mes_id = replymessage.to_uids where messageboard.from_uid =  ? ORDER BY replymessage.time DESC");
// getUnReadCommentarys
router.post("/api/getUnReadCommentarys",function(req,res,next){
  let isCheck = req.body.isCheck;
  let sql = "SELECT * FROM `messageboard` WHERE `mesIsCheck` = '"+isCheck+"'";
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// insertLoginStart
router.post("/api/insertLoginStart",function(req,res,next){
  let user_id = req.body.user_id;
  let loginStart_time = req.body.loginStart_time;
  console.log(user_id);
  console.log(loginStart_time);
  // insert into table set field1 = value1,field2 = value2
  let sql = "insert into loginout set login_start_time = '"+loginStart_time+"',login_user_id = '"+user_id+"'";
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// insertLoginOut
router.post("/api/insertLoginOut",function(req,res,next){
  let user_id = req.body.user_id;
  let loginOut_time = req.body.loginOut_time;
  console.log(user_id);
  console.log(loginOut_time);
  // insert into table set field1 = value1,field2 = value2
  let sql = "insert into loginout set login_out_time = '"+loginOut_time+"',login_user_id = '"+user_id+"'";
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// updateLoginOut
router.post("/api/updateLoginOut",function(req,res,next){
  let user_id = req.body.user_id;
  let loginOut_time = req.body.loginOut_time;
  console.log(user_id);
  console.log(loginOut_time);
  // insert into table set field1 = value1,field2 = value2
  let sql = "UPDATE `loginout` SET `login_out_time`= '"+loginOut_time+"' WHERE `login_user_id` = "+user_id;
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// updateLoginStart
router.post("/api/updateLoginStart",function(req,res,next){
  let user_id = req.body.user_id;
  let loginStart_time = req.body.loginStart_time;
  console.log(user_id);
  console.log(loginStart_time);
  // insert into table set field1 = value1,field2 = value2
  let sql = "UPDATE `loginout` SET `login_start_time`= '"+loginStart_time+"' WHERE `login_user_id` = "+user_id;
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// insertPraisemecount
router.post("/api/insertPraisemecount",function(req,res,next){
  let user_id = req.body.user_id;
  let praiseSum = req.body.praiseSum;
  console.log(user_id);
  console.log(praiseSum);
  let sql = "INSERT INTO `praisemecount`( `sum`, `pmc_user_id`) VALUES ('"+praiseSum+"','"+user_id+"')";
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// updatePraisemecount
router.post("/api/updatePraisemecount",function(req,res,next){
  let user_id = req.body.user_id;
  let praiseSum = req.body.praiseSum;
  console.log(user_id);
  console.log(praiseSum);
  let sql = "UPDATE `praisemecount` SET `sum`= "+praiseSum+" where `pmc_user_id` =  "+user_id;
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// insertPraiseme
router.post("/api/insertPraiseme",function(req,res,next){
  let key = JSON.parse(req.body.newPrase);
  console.log(key);
  let sql = "INSERT INTO `praiseme`(Mmes_id,praise_userId,praiseme_time) VALUES ('"+key.Mmes_id+"','"+key.praise_userId+"','"+key.praiseme_time+"')";
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// updatePraiseme
router.post("/api/updatePraiseme",function(req,res,next){
  let key = JSON.parse(req.body.newPrase);
  console.log(key);
  let sql = "UPDATE `praiseme` SET `praiseme_time` = '"+key.praiseme_time+"' WHERE `Mmes_id` = '"+key.Mmes_id+"' and `praise_userId` = '"+key.praise_userId+"' ";
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// delPraiseme
router.post("/api/delPraiseme",function(req,res,next){
  let mes_id = req.body.mes_id;
  let user_id = req.body.user_id;
  console.log(mes_id);
  console.log(user_id);
  let sql = "DELETE FROM `praiseme` WHERE `Mmes_id` = '"+mes_id+"' and `praise_userId` = '"+user_id+"' ";
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// topic_type查询留言板数据表内容：getCommentary
router.post("/api/getCommentary",function(req,res,next){
  let key = req.body;
  console.log(key);
  // let sql = "SELECT messageboard.* ,users.user_id,users.userType,users.nickname,users.avatar FROM users inner join messageboard on users.user_id = messageboard.from_uid where topic_type = '"+key.topicType+"' and topic_id = '"+key.id+"'  ORDER BY time DESC";
  let sql = "SELECT messageboard.*,users.user_id,users.userType,users.nickname,users.avatar,users.rank,praiseme.isPraise FROM messageboard INNER JOIN users on messageboard.from_uid = users.user_id LEFT JOIN praiseme ON praiseme.praise_userId = '"+key.user_id+"' AND praiseme.Mmes_id = messageboard.mes_id WHERE messageboard.topic_id = '"+key.id+"' and messageboard.topic_type = '"+key.topicType+"' ORDER BY time DESC"
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// 查询回复信息：getReplyCommentary
router.post("/api/getReplyCommentary",function(req,res,next){
  let to_uids = req.body.to_uids;
  //console.log(to_uids);
  let sql = "select replymessage.*,users.user_id,users.userType,users.nickname,users.avatar,users.rank from replymessage inner join users on replymessage.from_userId = users.user_id where to_uids = "+to_uids+" ORDER BY time DESC";
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// 添加回复：insertReply
router.post("/api/insertReply",function(req,res,next){
  let mes = JSON.parse(req.body.mes);
  let grade = req.body.grade;
  console.log(mes);
  console.log(grade);
  console.log(grade==1);
  let sql;
  if(grade==1){
    sql = "insert into replymessage(`repContent`,`time`,`from_userId`,`to_uids`) values ('"+mes.content+"','"+mes.time+"','"+mes.from_uid+"','"+mes.to_uids+"')"
  }else if(grade==2){
    sql = "insert into replymessage(`repContent`,`time`,`from_userId`,`to_uids`,`from_childId`,`f_cNickName`) values ('"+mes.content+"','"+mes.time+"','"+mes.from_uid+"','"+mes.to_uids+"','"+mes.fromChildId+"','"+mes.fromChildName+"')"
  }
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})

// 注册账号
router.post("/api/addUser",function(req,res,next){
  let key = req.body.user;
  //console.log(key);
// INSERT INTO `users`(`id`, `account`, `password`) VALUES (2,'11','111')
  let sql = "INSERT INTO `users`(`account`, `password`) VALUES ('"+key.email+"','"+key.pass+"')";
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// 添加密保问题 insertSecret
router.post("/api/insertSecret",function(req,res,next){
  let key = req.body.item;
  //console.log(key);
// INSERT INTO `secret`(`id`, `question`, `answer`, `user_id`) VALUES ([value-1],[value-2],[value-3],[value-4])
  let sql = "INSERT INTO `secret`(`question`, `answer`, `user_id`) VALUES ('"+key.question+"','"+key.answer+"','"+key.id+"')";
connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// 评论注册新账号：registerUser
router.post("/api/registerUser",function(req,res,next){
  let key = JSON.parse(req.body.newUser);
  //console.log(key);
// INSERT INTO `users`(`id`, `account`, `password`) VALUES (2,'11','111')
  let sql = "INSERT INTO `users`(`account`, `nickname`) VALUES ('"+key.account+"','"+key. nickname+"')";
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// 关键词搜索：searchKeyword
router.post("/api/searchKeyword",function(req,res,next){
  let key = req.body;
  //console.log(key);
// INSERT INTO `users`(`id`, `account`, `password`) VALUES (2,'11','111')
  let sql = "SELECT data_id,title FROM `data` where title like '%"+key.keywod+"%'";
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// 更新资料
router.post("/api/updateMes",function(req,res,next){
  let key = JSON.parse(req.body.user);
  console.log(key);
  // update  表名dao set 列名1= 'value1', 列名2= 'value2', 列名3= 'value3' where 条件;
  let sql = "update users set account = '"+key.account+"', nickname = '"+key.nickname+"',avatar = '"+key.avatar+"',Introduction = '"+key.Introduction+"',sex = '"+key.sex+"',birthday = '"+key.birthday+"',district = '"+key.district+"',school = '"+key.school+"'  where user_id = "+key.user_id;
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// 添加评论：insertMessage
router.post("/api/insertMessage",function(req,res,next){
  let key = JSON.parse(req.body.mes);
  console.log(key);
  console.log(key.zd_id=='');
  console.log(key.zd_id==0);
  let sql;
  if(key.zd_id=='' || key.zd_id==0){
    sql = "INSERT INTO `messageboard`(`topic_id`, `topic_type`, `content`,`time`, `from_uid`) VALUES ('"+key.topic_id+"','"+key.topic_type+"','"+key.content+"','"+key.time+"','"+key.from_uid+"')";
  }else{
    sql = "INSERT INTO `messageboard`(`topic_id`, `topic_type`, `content`, `zd_id`, `zd_duration`, `time`, `from_uid`) VALUES ('"+key.topic_id+"','"+key.topic_type+"','"+key.content+"','"+key.zd_id+"','"+key.zd_duration+"','"+key.time+"','"+key.from_uid+"')";
  }
  
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// 修改违规评论 editMes
router.post("/api/editMes",function(req,res,next){
  let content = req.body.content;
  let mes_id = req.body.mes_id;
  console.log(content);
  console.log(mes_id);
  let sql = "UPDATE `messageboard` SET `content` = '"+content+"' WHERE `mes_id` = '"+mes_id+"'"
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// 修改评论 editCommentarys
router.post("/api/editCommentarys",function(req,res,next){
  let key = JSON.parse(req.body.edit);
  console.log(key);
  console.log(key.zd_id=='');
  console.log(key.zd_id==0);
  let sql;
  if(key.zd_id=='' || key.zd_id==0){
    sql = "UPDATE `messageboard` SET topic_id = '"+key.topic_id+"',topic_type = '"+key.topic_type+"',content = '"+key.content+"' where mes_id = '"+key.mes_id+"'";
  }else{
    sql = "UPDATE `messageboard` SET topic_id = '"+key.topic_id+"',topic_type = '"+key.topic_type+"',content = '"+key.content+"',zd_id = '"+key.zd_id+"',zd_duration = '"+key.zd_duration+"' where mes_id = '"+key.mes_id+"'";
  }
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// 删除评论 deleteReview
router.post("/api/deleteReview",function(req,res,next){
  let type = req.body.type;
  let id = req.body.mesId;
  //console.log("----------删除评论---------------");
  //console.log(type);
  //console.log(id);
  let sql;
  if(type==1){
    sql = "DELETE FROM `messageboard` WHERE `mes_id` = "+id;
  }else if(type==2){
    sql = "DELETE FROM `replymessage` WHERE `reId` = "+id;
  }
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// 留言页点赞效果：updateMesPraise
router.post("/api/updateMesPraise",function(req,res,next){
  let key = JSON.parse(req.body.mesObj);
  //console.log(key);
  let sql = "";
  // 语法一:UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值
  if(key.praiseType==null){
    sql = "update `messageboard` set praiseCount = "+(Number(key.praiseCount)+1)+" where mes_id = "+key.mes_id;
  }else{
    sql = "update `messageboard` set praiseCount = "+(Number(key.praiseCount)-1)+" where mes_id = "+key.mes_id;
  }
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// updateDataPCount
router.post("/api/updateDataPCount",function(req,res,next){
  let key = JSON.parse(req.body.mesObj);
  //console.log(key);
  // //console.log(key.dataPraise==0);
  let sql = "";
  // 语法一:UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值
  if(key.dataPraise==null){
    sql = "update `data` SET  dPraiseCount = "+(Number(key.dPraiseCount)+1)+" where data_id = "+key.data_id;
  }else{
    sql = "update  `data` SET dPraiseCount = "+(Number(key.dPraiseCount)-1)+" where data_id = "+key.data_id;
  }
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// selUnReadMes
router.post("/api/selUnReadMes",function(req,res,next){
  let isCheck = req.body.isCheck;
  let key = JSON.parse(req.body.commentarys);
  console.log(isCheck);
  console.log(key);
  let sql;
  let pagenum = Number(key.pagenum);
  let pagesize = Number(key.pagesize);
  //console.log(typeof(pagenum));
  //console.log(typeof(pagesize));
  //console.log(key.query=='');
  if(key.query==''){
    sql = "SELECT messageboard.*,users.account FROM `messageboard` left join users on messageboard.from_uid = users.user_id WHERE messageboard.`mesIsCheck` = '"+isCheck+"' limit "+((pagenum-1)*pagesize)+","+pagesize;
  }else{
    sql = "SELECT messageboard.*,users.account FROM `messageboard`  left join users on messageboard.from_uid = users.user_id  where messageboard.`mesIsCheck` = '"+isCheck+"' and messageboard.content like '%"+key.query+"%' or messageboard.topic_type like '%"+key.query+"%' or users.nickname like '%"+key.query+"%' limit "+((pagenum-1)*pagesize)+","+pagesize;
  }

  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// 详情页页收藏效果：updatedcCount
router.post("/api/updatedcCount",function(req,res,next){
  let key = JSON.parse(req.body.mesObj);
  //console.log(key);
  //console.log(key.dataCollect==0);
  let sql = "";
  // 语法一:UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值
  if(key.dataCollect==null){
    sql = "update `data` SET  dCollectCount = "+(Number(key.dCollectCount)+1)+" where data_id = "+key.data_id;
  }else{
    sql = "update `data` SET dCollectCount = "+(Number(key.dCollectCount)-1)+" where data_id = "+key.data_id;
  }
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// 添加点赞数据：insertPraise
// router.post("/api/insertPraise",function(req,res,next){
//   let key = JSON.parse(req.body.praiseObj);
//   //console.log(key);
//   let sql = "insert into `praise` set type = '"+key.type+"',typeId = '"+key.typeId+"',message_id = '"+key.message_id+"',uid = '"+key.uid+"'";
//   connection.query(sql,function(error,result,fields){
//     if(error){
//       res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
//       status:400,message:"请求失败"})
//     }else{
//       res.json({
//         //xhr.status:获取当前服务器的响应状态  200=>成功
//         status:200,
//         message:result
//       })
//     }
//   })
// })
// 添加收藏数据：insertCollect
// router.post("/api/insertCollect",function(req,res,next){
//   let key = JSON.parse(req.body.collectObj);
//   //console.log(key);
//   let sql = "INSERT INTO `collect` set typeId = '"+key.typeId+"',type = '"+key.type+"',data_id = '"+key.data_id+"',uid = '"+key.uid+"'";
//   connection.query(sql,function(error,result,fields){
//     if(error){
//       res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
//       status:400,message:"请求失败"})
//     }else{
//       res.json({
//         //xhr.status:获取当前服务器的响应状态  200=>成功
//         status:200,
//         message:result
//       })
//     }
//   })
// })
// // 查询点赞数据：selPraise
// router.post("/api/selPraiseMes",function(req,res,next){
//   let key = req.body;
//   //console.log(key);
//   let sql = "select * from `praise` where uid = '"+key.id+"' and type = '"+key.type+"'";
//   connection.query(sql,function(error,result,fields){
//     if(error){
//       res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
//       status:400,message:"请求失败"})
//     }else{
//       res.json({
//         //xhr.status:获取当前服务器的响应状态  200=>成功
//         status:200,
//         message:result
//       })
//     }
//   })
// })
// 查询收藏数据：selCollect
// router.post("/api/selCollect",function(req,res,next){
//   let key = req.body;
//   //console.log("查询收藏数据：");
//   //console.log(key);
//   let sql = "select * FROM `collect` where uid = '"+key.id+"' and type = '"+key.type+"'";
//   connection.query(sql,function(error,result,fields){
//     if(error){
//       res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
//       status:400,message:"请求失败"})
//     }else{
//       res.json({
//         //xhr.status:获取当前服务器的响应状态  200=>成功
//         status:200,
//         message:result
//       })
//     }
//   })
// })
// 修改点赞数据：updatePraise
// router.post("/api/updatePraise",function(req,res,next){
//   let key = req.body;
//   //console.log("这里是更改点赞数据");
//   //console.log(key);
//   let sql = "UPDATE `praise` SET `message_id`= '"+key.praiseMesId+"' where type = '"+key.type+"' and  `uid`= '"+key.uid+"'";
//   connection.query(sql,function(error,result,fields){
//     if(error){
//       res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
//       status:400,message:"请求失败"})
//     }else{
//       res.json({
//         //xhr.status:获取当前服务器的响应状态  200=>成功
//         status:200,
//         message:result
//       })
//     }
//   })
// })
// 修改点赞数据：updateCollect
// router.post("/api/updateCollect",function(req,res,next){
//   let key = req.body;
//   //console.log("这里是更改收藏数据");
//   //console.log(key);
//   // let sql = "UPDATE `praise` SET `message_id`= '"+key.praiseMesId+"' where type = '"+key.type+"' and  `uid`= '"+key.uid+"'";
//   let sql = "UPDATE `collect` SET data_id = '"+key.collectMesId+"' where type = '"+key.type+"' and uid = '"+key.id+"'";
//   connection.query(sql,function(error,result,fields){
//     if(error){
//       res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
//       status:400,message:"请求失败"})
//     }else{
//       res.json({
//         //xhr.status:获取当前服务器的响应状态  200=>成功
//         status:200,
//         message:result
//       })
//     }
//   })
// })
// 登录初，更改点赞信息：updatePraiseType
// router.post("/api/updatePraiseType",function(req,res,next){
//   let key = req.body;
//   //console.log(key);
//   let sql = "UPDATE `messageboard` SET `praiseType`= 1 where mes_id in ("+key.praiseId+")";
//   connection.query(sql,function(error,result,fields){
//     if(error){
//       res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
//       status:400,message:"请求失败"})
//     }else{
//       res.json({
//         //xhr.status:获取当前服务器的响应状态  200=>成功
//         status:200,
//         message:result
//       })
//     }
//   })
// })
// 登录初，更改点赞信息：updatePraiseL
// router.post("/api/updatePraiseL",function(req,res,next){
//   let key = req.body;
//   //console.log(key);
//   let sql = "UPDATE `data` SET `dataPraise` = '1' where data_id in ("+key.praiseId+")";
//   connection.query(sql,function(error,result,fields){
//     if(error){
//       res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
//       status:400,message:"请求失败"})
//     }else{
//       res.json({
//         //xhr.status:获取当前服务器的响应状态  200=>成功
//         status:200,
//         message:result
//       })
//     }
//   })
// })
// 登录初，更改收藏信息：updateCollectL
router.post("/api/updateCollectL",function(req,res,next){
  let key = req.body;
  //console.log(key);
  let sql = "UPDATE `data` SET `dataCollect`= 1 where data_id in ("+key.collectId+")";
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// 浏览数adddDetailCount
router.post("/api/adddDetailCount",function(req,res,next){
  let key = req.body;
  //console.log(key);
  let sql = "UPDATE `data` SET `readAmount` = "+(Number(key.readAmount)+1)+" where data_id = "+key.id;
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// 更改账号或密码 changeMes
router.post("/api/changeMes",function(req,res,next){
  let key = req.body.oldUser;
  //console.log(key);
  // UPDATE `users` SET `password` = '121' WHERE account = 'ceshiha@qq.com'
  let sql = "UPDATE `users` SET `password` = '"+key.pass+"' WHERE account = '"+key.email+"'";
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// 添加文章 insertPosts
router.post("/api/insertPosts",function(req,res,next){
  let key = JSON.parse(req.body.Posts);
  console.log(key);   
  // UPDATE `users` SET `password` = '121' WHERE account = 'ceshiha@qq.com'
  let sql = "INSERT INTO `data`( `title`, `describes`, `time`, `content`, `Sort_id`, `contentSort_id`) VALUES ('"+key.title+"','"+key.describe+"','"+key.time+"','"+key.content+"','"+key.Sort_id+"','"+key.contentSort_id+"')";
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// 修改文章 editPosts
router.post("/api/editPosts",function(req,res,next){
  let key = JSON.parse(req.body.edit);
  //console.log("------------------");
  //console.log(key);
  // UPDATE `users` SET `password` = '121' WHERE account = 'ceshiha@qq.com'
  let sql = "UPDATE `data` SET `title` = '"+key.title+"',`describes` = '"+key.describe+"',`img`='"+key.img+"',`content`='"+key.content+"',`Sort_id`='"+key.Sort_id+"',`contentSort_id`='"+key.contentSort_id+"' where data_id = '"+key.data_id+"'";
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// 删除文章 delPosts
router.post("/api/delPosts",function(req,res,next){
  let key = req.body.id;
  //console.log("------------------");
  //console.log(key);
  let sql = "DELETE FROM `data` WHERE data_id = "+key;
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// 查询文章 selPostsDesc
router.post("/api/selPostsDesc",function(req,res,next){
  let key = req.body.queryPosts;
  //console.log(key);
  // let sql;
  let pagenum = Number(key.pagenum);
  let pagesize = Number(key.pagesize);
  //console.log(typeof(pagenum));
  //console.log(typeof(pagesize));
  //console.log(key.query=='');
  if(key.query==''){
    sql = "select data.*,`contentsort`.`ctName`, `sort`.`sortName` from contentsort INNER JOIN data ON contentsort.consort_id = data.contentSort_id INNER JOIN sort ON data.Sort_id = sort.sort_id  LIMIT "+((pagenum-1)*pagesize)+","+pagesize;
  }else{
    sql = "select data.*,`contentsort`.`ctName`, `sort`.`sortName` from contentsort INNER JOIN data ON contentsort.consort_id = data.contentSort_id INNER JOIN sort ON data.Sort_id = sort.sort_id WHERE data.title like '%"+key.query+"%' or data.describe like '%"+key.query+"%' LIMIT "+((pagenum-1)*pagesize)+","+pagesize;
  }
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// 查询标签 selTags
router.post("/api/selTags",function(req,res,next){
  let key = req.body.queryTags;
  //console.log(key);
  let sql;
  let pagenum = Number(key.pagenum);
  let pagesize = Number(key.pagesize);
  //console.log(typeof(pagenum));
  //console.log(typeof(pagesize));
  //console.log(key.query=='');
  if(key.query==''){
    sql = "SELECT * FROM `sort` limit "+((pagenum-1)*pagesize)+","+pagesize;
  }else{
    sql = "SELECT * FROM `sort` where sortName like '%"+key.query+"%' or remarks like '%"+key.query+"%' limit "+((pagenum-1)*pagesize)+","+pagesize;
  }
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// 添加标签 insertTags
router.post("/api/insertTags",function(req,res,next){
  let key = JSON.parse(req.body.addTags);
  //console.log(key);
  let sql = "INSERT INTO `sort`( `sortName`, `remarks`) VALUES ('"+key.sortName+"','"+key.remarks+"')";
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// 修改标签 editTags
router.post("/api/editTags",function(req,res,next){
  let key = JSON.parse(req.body.edit);
  //console.log("tags-----------");
  //console.log(key);
  let sql = "UPDATE `sort` SET sortName = '"+key.sortName+"',remarks = '"+key.remarks+"' where sort_id = "+key.sort_id;
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// 删除标签 delTags
router.post("/api/delTags",function(req,res,next){
  let key = req.body.id;
  //console.log("del-----------");
  //console.log(key);
  let sql = "DELETE FROM `sort` WHERE sort_id = "+key;
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// 查询分类 selSorts
router.post("/api/selSorts",function(req,res,next){
  let key = req.body.querySorts;
  //console.log("sel-----------");
  //console.log(key);
  let sql;
  let pagenum = Number(key.pagenum);
  let pagesize = Number(key.pagesize);
  // //console.log(typeof(pagenum));
  // //console.log(typeof(pagesize));
  //console.log(key.query=='');
  if(key.query==''){
    sql = "SELECT * FROM `contentsort` limit "+((pagenum-1)*pagesize)+","+pagesize;
  }else{
    sql = "SELECT * FROM `contentsort` where ctName like '%"+key.query+"%' or remarks like '%"+key.query+"%' limit "+((pagenum-1)*pagesize)+","+pagesize;
  }
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// 添加分类 insertSorts
router.post("/api/insertSorts",function(req,res,next){
  let key = req.body.addSorts;
  //console.log("add-----------");
  //console.log(key);
  let sql = "INSERT INTO `contentsort`( `ctName`, `remarks`) VALUES ('"+key.ctName+"','"+key.remarks+"')"
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// 修改分类 editSorts
router.post("/api/editSorts",function(req,res,next){
  let key = req.body.edit;
  //console.log("editSorts-----------");
  //console.log(key);
  let sql = "UPDATE `contentsort` SET `ctName` = '"+key.ctName+"',`remarks`= '"+key.remarks+"' WHERE consort_id = "+key.consort_id;
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// 删除分类 delSorts
router.post("/api/delSorts",function(req,res,next){
  let key = req.body.id;
  //console.log("delSorts-----------");
  //console.log(key);
  let sql = "DELETE FROM `contentsort` WHERE consort_id = "+key;
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// 查询用户 selUser
router.post("/api/selUser",function(req,res,next){
  let key = req.body.queryUsers;
  //console.log("sel-----------");
  //console.log(key);
  let sql;
  let pagenum = Number(key.pagenum);
  let pagesize = Number(key.pagesize);
  //console.log(typeof(pagenum));
  //console.log(typeof(pagesize));
  //console.log(key.query=='');
  if(key.query==''){
    sql = "SELECT * FROM `users` limit "+((pagenum-1)*pagesize)+","+pagesize;
  }else{
    sql = "SELECT * FROM `users` where account like '%"+key.query+"%' or nickname like '%"+key.query+"%' limit "+((pagenum-1)*pagesize)+","+pagesize;
  }
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// 添加用户 insertUser
router.post("/api/insertUser",function(req,res,next){
  let key = JSON.parse(req.body.addUser);
  //console.log("addUser-----------");
  //console.log(key);
  let sql = "INSERT INTO `users`(`account`, `password`, `userType`, `regtime`, `nickname`, `avatar`, `Introduction`, `sex`, `birthday`, `district`, `school`) VALUES ('"+key.account+"','"+key.password+"','"+key.userType+"','"+key.regtime+"','"+key.nickname+"','"+key.avatar+"','"+key.Introduction+"','"+key.sex+"','"+key.birthday+"','"+key.district+"','"+key.school+"')";
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// 修改用户 editUser
router.post("/api/editUser",function(req,res,next){
  let key = JSON.parse(req.body.edit);
  //console.log("edit-----------");
  //console.log(key);
  let sql = "UPDATE `users` SET `account`= '"+key.account+"',`password`= '"+key.password+"',`userType`= '"+key.userType+"',`regtime`= '"+key.regtime+"',`nickname`= '"+key.nickname+"',`avatar`= '"+key.avatar+"',`Introduction`= '"+key.Introduction+"',`sex`= '"+key.birthday+"',`birthday`= '"+key.birthday+"',`district`= '"+key.district+"',`school`= '"+key.school+"' WHERE user_id = "+key.user_id;
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// 查询评论 selCommentarys
router.post("/api/selCommentarys",function(req,res,next){
  let key = req.body.commentarys;
  //console.log("sel-----------");
  console.log(key);
  let sql;
  let pagenum = Number(key.pagenum);
  let pagesize = Number(key.pagesize);
  //console.log(typeof(pagenum));
  //console.log(typeof(pagesize));
  //console.log(key.query=='');
  if(key.query==''){
    sql = "SELECT messageboard.*,users.account FROM `messageboard` left join users on messageboard.from_uid = users.user_id limit "+((pagenum-1)*pagesize)+","+pagesize;
  }else{
    sql = "SELECT messageboard.*,users.account FROM `messageboard`  left join users on messageboard.from_uid = users.user_id  where messageboard.content like '%"+key.query+"%' or messageboard.topic_type like '%"+key.query+"%' or users.nickname like '%"+key.query+"%' limit "+((pagenum-1)*pagesize)+","+pagesize;
  }
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// 查询评论的typeId selMesTypeId
router.get("/api/selMesTypeId",function(req,res,next){
  let sql = "select distinct topic_type from messageboard"
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// 查询密保问题 selRetrievePass
router.post("/api/selRetrievePass",function(req,res,next){
  let key = req.body.form;
  //console.log(key);
  // UPDATE `users` SET `password` = '121' WHERE account = 'ceshiha@qq.com'
  let sql = "select `secret`.question,`secret`.answer,`users`.password from `users` inner join `secret` on users.user_id=secret.user_id where account = '"+key.email+"'";
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      // //console.log(result.length);
      let mes = Verify(result)
      //console.log(mes);
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:mes
      })
    }
  })
})

var Verify = function(res,question,answer){
  let result;
  if(res.length==0){
    result="您还没有设置密保问题哦，不能找回密码，请在留言板联系博主找回密码吧"
    return result
  }else{
    return res
    
  }
}
// 查询菜单 selMenu
router.get("/api/selMenu",function(req,res,next){
  let sql = "SELECT * FROM `menu`";
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
//  selPraisemeMessage
router.post("/api/selPraisemeMessage",function(req,res,next){
  let mes_id = req.body.mes_id;
  let user_id = req.body.user_id;
  console.log("sel-----------");
  console.log(mes_id);
  console.log(user_id);
  let sql = "SELECT * FROM `praiseme` WHERE `Mmes_id` = '"+mes_id+"' and `praise_userId` = "+user_id;
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
//  insertCeshi
router.post("/api/insertCeshi",function(req,res,next){
  let imgUrl = req.body.imgUrl;
  console.log(imgUrl);
  let sql = "INSERT INTO `praisemecount`( `img`) VALUES ('"+imgUrl+"')";
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// 查询data表的详情 getDataDetail
router.post("/api/getDataDetail",function(req,res,next){
  let key = req.body;
  console.log(key);
  let sql = "SELECT data.*,datapraise.dp_isPraise,datacollect.dc_IsCollect FROM `data` LEFT JOIN datapraise ON datapraise.dataP_id = data.data_id AND datapraise.dp_userId = '"+key.user_id+"' LEFT JOIN datacollect ON datacollect.dataC_id = data.data_id AND datacollect.dc_userId = '"+key.user_id+"' where data_id = '"+key.data_id+"'";
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// 查询dataPraise表中当前登录的用户是否点击过该内容 selDataPraiseMes
router.post("/api/selDataPraiseMes",function(req,res,next){
  let key = req.body;
  console.log(key);
  let sql = "SELECT * FROM `datapraise` WHERE `dataP_id` = '"+key.data_id+"' AND `dp_userId` = '"+key.user_id+"'";
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// dataPraise表中添加点赞信息
router.post("/api/insertDataPraise",function(req,res,next){
  let key = JSON.parse(req.body.newPraise);
  console.log(key);
  let sql = "INSERT INTO `datapraise`( `dataP_id`, `dp_userId`, `dataPraise_time`) VALUES ('"+key.dataP_id+"','"+key.dp_userId+"','"+key.dataPraise_time+"')";
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// dataPraise表中修改点赞信息
router.post("/api/updateDataPraise",function(req,res,next){
  let key = JSON.parse(req.body.newPraise);
  console.log(key);
  let sql = "UPDATE `datapraise` SET `dataP_id` = '"+key.dataP_id+"',`dataPraise_time` = '"+key.dataPraise_time+"' WHERE dataP_id = '"+key.dataP_id+"' `dp_userId` = "+key.dp_userId;
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// dataPraise表中删除点赞信息
router.post("/api/delDataPraise",function(req,res,next){
  let key = req.body;
  console.log(key);
  let sql = "DELETE FROM `datapraise` WHERE `dataP_id` = '"+key.data_id+"' AND `dp_userId` = '"+key.user_id+"'";
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// 查询dataCollect表中当前登录的用户是否点击过该内容 selDataCollectMes
router.post("/api/selDataCollectMes",function(req,res,next){
  let key = req.body;
  console.log(key);
  let sql = "SELECT * FROM `datacollect` WHERE `dataC_id` = '"+key.data_id+"' AND `dc_userId` = '"+key.user_id+"'";
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// dataCollect 中添加信息
router.post("/api/insertDataCollect",function(req,res,next){
  let key = JSON.parse(req.body.newCollect);
  console.log(key);
  let sql = "INSERT INTO `datacollect`( `dataC_id`, `dc_userId`, `dataCollect_time`) VALUES ('"+key.dataC_id+"','"+key.dc_userId+"','"+key.dataCollect_time+"')";
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// dataCollect表中修改收藏信息
router.post("/api/updateDataCollect",function(req,res,next){
  let key = JSON.parse(req.body.newPraise);
  console.log(key);
  let sql = "UPDATE `datacollect` SET `dataCollect_time` = '"+key.dataCollect_time+"' WHERE  `dataC_id` = '"+key.dataC_id+"',`dc_userId` = "+key.dc_userId;
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// dataCollect表中删除点赞信息
router.post("/api/delDataCollect",function(req,res,next){
  let key = req.body;
  console.log(key);
  let sql = "DELETE FROM `datacollect` WHERE `dataC_id` = '"+key.data_id+"' AND `dc_userId` = '"+key.user_id+"'";
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// selReviewPraise
router.post("/api/selReviewPraise",function(req,res,next){
  let key = req.body;
  console.log("----user_id");
  console.log(key);
  let sql = "SELECT messageboard.*,users.user_id,users.userType,users.nickname,users.avatar,users.rank,praiseme.praiseMeId FROM messageboard INNER JOIN users on messageboard.from_uid = users.user_id LEFT JOIN praiseme ON praiseme.praise_userId = '"+key.user_id+"' AND praiseme.Mmes_id = messageboard.mes_id WHERE praiseme.isPraise ='1' ORDER BY time DESC";
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// selArticlePraise
router.post("/api/selArticlePraise",function(req,res,next){
  let key = req.body;
  console.log("----user_id");
  console.log(key);
  let sql = "SELECT data.*,datapraise.dataPraiseId FROM data LEFT JOIN datapraise ON data.data_id = datapraise.dataP_id  and datapraise.dp_userId = '"+key.user_id+"' WHERE datapraise.dp_isPraise ='1' ORDER BY dataPraise_time DESC";
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// selTCollect
router.post("/api/selTCollect",function(req,res,next){
  let key = req.body;
  console.log("----user_id");
  console.log(key);
  let sql = "SELECT data.*,datacollect.dataCollectId FROM data LEFT JOIN datacollect ON data.data_id = datacollect.dataC_id  and datacollect.dc_userId = '"+key.user_id+"' WHERE datacollect.dc_IsCollect ='1' ORDER BY dataCollect_time DESC";
  connection.query(sql,function(error,result,fields){
    if(error){
      res.json({//xhr.status:获取当前服务器的响应状态  400=>成功
      status:400,message:"请求失败"})
    }else{
      res.json({
        //xhr.status:获取当前服务器的响应状态  200=>成功
        status:200,
        message:result
      })
    }
  })
})
// 全局查询知识标签数据
funSelect("/api/selCeshi","SELECT * FROM `praisemecount` LIMIT 1")
module.exports = router;
